////--------------------------国检公共
function changeYth(v){
	var thdh = $("#thdh");
	var sljg = $("#sljgSelect");
	var ydsl = $("#ydsl");
	var ythVlue = $("#ythVlue");
	 if($(v).attr("checked")=="checked"){
		 ythVlue.attr("value","Y");
		 
		 thdh.removeAttr("readonly");
		 try{
			 sljg.combogrid("readonly", false);
		 }catch(e){
			 initByInputObject(sljg);
			 sljg.combogrid("readonly", false);
		 }
		 ydsl.combobox("enable"); 
	 }else {
		 ythVlue.attr("value","N");
		 
		 thdh.attr("value","");
		 thdh.attr("readonly","readonly");
		 
		 clearSelectValue(sljg);
		 sljg.combogrid("readonly", true);
		 $("#sljgdm").attr("value","");
		 //$("#ydsl span input.combo-text").attr("readonly",true);
		 ydsl.combobox("clear").combobox("disable");
	 }
	
}

function copyJyjyNewOne() {
	var gjh =  $("#gjhInput").attr("value");
    $("#jyjyId").val("");
    $("#gjhInput").attr("value", "");
    $("#gjglmmInput").attr("value", "");
    $("#qsbhInput").attr("value", "");
    $("#bjhInput").attr("value", "");
    $("#tgdhInput").attr("value", "");
    $("#bjsbrqInput").attr("value", "");
    $("#jyjybjsbrq").attr("value", "");
    $("#JYISLOCK").attr("value", "");
    changeGjStatuName("");
    $("input[id^='good_id_']").each(function(){
    	$(this).attr("value","");
    });
    $("input[id^='good_spxh_']").each(function(){
    	$(this).attr("value","");
    });
    $("input[id^='box_id_']").each(function(){
    	$(this).attr("value","");
    });
    if(gjh && gjh !=""){
    	resetIframe("复制"+gjh);
    }
    
}

///------用于国检 提交表单时 无效的原因.
function addDialogToForm() {
    openSfdj().dialog("close");
    openXydz().dialog("close");
    var result = "1=1&";
    var sfdjResult = $("#sfdjForm").serialize();
    var sxdjResult = $("#sxdjForm").serialize();
    var thlxResult = $("#thlxForm").serialize();
    if (sfdjResult) {
        result += sfdjResult + "&";
    }

    if (sxdjResult) {
        result += sxdjResult + "&";
    }
    if (thlxResult) {
        result += thlxResult + "&";
    }
    return result;

}

//--------------添加商品
var goodSerIdIndex=1;
var good_edit_serid ="";

function copyGood(){
	setGoodSerId(goodSerIdIndex);
	clearJyTrClassSuccess();
    good_edit_serid = "";
    $("#good_addButton").val("添加");
    $("#good_id").attr("value","");
    $("#good_spxh").attr("value","");
    reSetShowSerid();
}

function clearJyTrClassSuccess(){
	$("#goodshow_serid_"+good_edit_serid).parent().parent().removeClass("success");
}


function setGoodSerId(goodSerIdIndex) {
    $("#good_serid").val(goodSerIdIndex);
}
function addOrUpdateGood() {
	var flag;
    if (good_edit_serid != null && good_edit_serid != "") {
      flag = updateGoodTr(good_edit_serid);
    } else {
      flag = addGood();
    }
    if(flag == 1){
    	if($("#ciqSelect").attr("isinit") && $("#ciqSelect").attr("isinit") == 1) {
            $("#ciqSelect").combogrid("clear");
        }
    }
}

function validateGood() {
    return validateBase("goodRequiredClass");
}

function addOrUpdateGoodValidate() {
    if (calculateSyl(1) != 1) {
        return 0;
    }
    return validateGood();
}

function addGood() {
    if (addOrUpdateGoodValidate() != 1) {
        return 0;
    }
    var addHtml = getNewGoodHtml(goodSerIdIndex);
    $("#good_lastTR").before(addHtml);
    backUpXkzId(goodSerIdIndex);
    backUpHwxxjlId(goodSerIdIndex);
    goodSerIdIndex = goodSerIdIndex + 1;
    setGoodSerId(goodSerIdIndex);
    changeGoodXkzInput();
    initGoodInput();
    clearJyBjslAndBjzlRequredClass();
    focusJyAndGjHSCode();
    reSetShowSerid();
    return 1;
}


function getNewGoodHtml(serid) {
    var result = "<tr id='good_TR" + serid + "' onclick='editGoodTr(" + serid + ")'>";
    var namePre = "jyjy.goods[" + serid + "].";

    result += addTr("good_input_table", serid, namePre);
    result += "<td  style='text-align:center;'><img src=\"../image/delete.gif\" onclick='deleteJyGoodTr("+ serid + ")'>";
    result += getXkzDivHtml(serid);
    result += getHwxxjlDivHtml(serid) + "</td>";
    result += "</tr>";
    return result;
}

function deleteJyGoodTr(serid){
	delTr("good_TR" + serid ); 
	clearJyTrClassSuccess();
	initGoodInput();
	reSetShowSerid();
	
}
function getBaseInput(showContentId, isRemove) {
    var result = "";
    $("#" + showContentId + " tr[onclick]").each(function() {
        result += $(this).prop("outerHTML");
        if (isRemove == 1) {
            $(this).remove();
        }
    });

    return result;
}

var backUpAft = "_backUp";

var newGoodHwxxjlTablePreName = "good_hwxxjls_div_";
var hwxxjl_show_table = "hwxxjl_show_table";
var hwxxjl_lasttr = "hwxxjl_lasttr";
var hwxxjl_tr = "hwxxjl_tr";
var hwxxjl_input_div = "hwxxjl_input_div";

var newGoodXkzTablePreName = "good_xkzs_div_";
var xkz_show_table = "xkz_show_table";
var xkz_lasttr = "xkz_lasttr";
var xkz_tr = "xkz_tr";
var xkz_input_div = "xkz_input_div";

function getXkzInput() {
    return getBaseInput(xkz_show_table, 1);
}
function getHwxxjlInput() {

    return getBaseInput(hwxxjl_show_table, 1);
}

function getXkzDivHtml(serid) {
    var result = "<table id='" + newGoodXkzTablePreName + serid + "' style='display:none;' >";
    result += getXkzInput();
    result += "</table>";
    return result;
}

function getHwxxjlDivHtml(serid) {
    var result = "<table id='" + newGoodHwxxjlTablePreName + serid + "' style='display:none;' >";
    result += getHwxxjlInput();
    result += "</table>";
    return result;
}

function setXkzDivHtml(serid) {
    var div = $("#" + newGoodXkzTablePreName + serid);
    if (div) {
        var xkzHtml = getXkzInput();
        div.html(xkzHtml);
        backUpXkzId(serid);
    }
}
function setHwxxjlDivHtml(serid) {
    var div = $("#" + newGoodHwxxjlTablePreName + serid);
    if (div) {
        var html = getHwxxjlInput();
        div.html(html);
        backUpHwxxjlId(serid);
    }
}

function backUpXkzId(serid) {
    var divId = newGoodXkzTablePreName + serid;
    backUpBaseId(divId);
}
function backUpHwxxjlId(serid) {
    var divId = newGoodHwxxjlTablePreName + serid;
    backUpBaseId(divId);
}

function backUpBaseId(elemContentId) {
    $("#" + elemContentId + " *[id]").each(function() {
        var elemId = $(this).attr("id");
        $(this).attr("id", elemId + backUpAft);
    });
}

function reSetXkzId(divId) {
    reSetBaseId(divId);
}
function reSetHwxxjlId(divId) {
    reSetBaseId(divId);
}
function reSetBaseId(divId) {
    $("#" + divId + " *[id]").each(function() {
        var elemId = $(this).attr("id");
        $(this).attr("id", elemId.replace(backUpAft, ""));
    });
}

function setXkzInput(serid) {
    var divId = newGoodXkzTablePreName + serid;
    var lastTrId = xkz_lasttr;
    var toContentId = xkz_show_table;
    setBaseInput(divId, lastTrId, toContentId);
}
function setHwxxjlInput(serid) {
    var divId = newGoodHwxxjlTablePreName + serid;
    var lastTrId = hwxxjl_lasttr;
    var toContentId = hwxxjl_show_table;
    setBaseInput(divId, lastTrId, toContentId);
}

function setBaseInput(divId, lastTrId, toContentId) {
    var innerHtml = "";
    $("#" + divId + " tr[onclick]").each(function() {
        innerHtml += $(this)[0].outerHTML;
    });
    $("#" + toContentId + " tr[onclick]").each(function() {
        $(this).remove();
    });
    $("#" + lastTrId).before(innerHtml);
    reSetBaseId(toContentId);
}

function editGoodTr(trSerNo) {
	isSelectCiqFirstRow = 0;
    unlockGoods("commonGoods");
    good_edit_serid = "" + trSerNo;
    setIsHsCodeEdit(1);
    //var time1 = new Date().getTime();
    reSetGoodTrSelfSelected(trSerNo);
    //var time2 = new Date().getTime(); 
   // alert($("#good_show_serid_"+trSerNo).attr("value")+","+$("#good_show_serid_"+trSerNo).val());
    editTr("good_TR" + trSerNo);
    //alert($("#good_show_serid").attr("value")+","+$("#good_show_serid").val());
    setXkzInput(trSerNo);
    reSetXkzId(xkz_show_table);
    setHwxxjlInput(trSerNo);
    reSetHwxxjlId(hwxxjl_show_table);
    $("#good_addButton").val("修改");
    initXkzInput();
    initHwxxjlInput();
    changeGoodXkzInput();
    var goodHsbhMc = $("#good_hsbhmc").attr("value");
    if( ! goodHsbhMc){
        goodHsbhMc = $("#hggoods_spbhdm").attr("value"); 
    }
    setJyjyCaculate(goodHsbhMc);
    adjustBaoZhuanSuLiangRequred(goodHsbhMc);
    setHgJySetDejldwRequiredClass();
    setIsHsCodeEdit(0);
    lockGoods("commonGoods");
}

function setHgJySetDejldwRequiredClass() {
   
        var hsCode = $("#hggoods_spbhdm").attr("value");
        if(! hsCode){
        	hsCode = $("#good_hsbhmc").attr("value");
        }
        if(hsCode){
        	setHgDejldwRequired(hsCode);
        }
        
}

function reSetGoodTrSelfSelected(trSerNo) {
    editSHowHgGoodsCombogridSelect(trSerNo);
    var selectIds = ["goodSelect_ycd", "goodSelect_bzzl", "goodSelect_yt", "goodSelect_yztj", "goodSelect_bz", "goodSelect_sldw", "goodSelect_hsbm", "goodSelect_fzfss", "ciqSelect", "good_zldwSelect", "goodSelect_bzldw"];
    var valueInputIds = ["good_ycd", "good_bzzl", "good_yt", "good_yztj", "good_bz", "good_sldw", "good_hsbh", "good_fzbzdm", "good_ciqdm", "good_zldw", "good_bzldw"];
    var textInputIds = ["good_ycdmc", "good_bzzlmc", "good_ytmc", "good_yztjmc", "good_bzmc", "good_sldwmc", "good_hsbhmc", "good_fzbzmc", "good_ciqdmCode", "good_zldwmc", "good_bzldwmc"];
    var myValue;
    var myText;
    for (var index = 0; index < selectIds.length; index++) {
        myValue = $("#" + valueInputIds[index] + "_" + trSerNo).val();
        myText = $("#" + textInputIds[index] + "_" + trSerNo).val();
        // alert(selectIds[index]+":"+myText+","+myValue);
       // setSelectValue($("#" + selectIds[index]),valueInputIds[index],textInputIds[index],valueInputIds[index] + "_" + trSerNo,textInputIds[index] + "_" + trSerNo);
      setJySelectValue($("#" + selectIds[index]),myValue,myText); 
   //   initSelectTextAndValue($("#" + selectIds[index]), myText, myValue);
    }
    if($("#ciqSelect").attr("isInit") && $("#ciqSelect").attr("isInit") == 1) {
       $("#ciqSelect").combogrid("showPanel").combogrid("hidePanel");//防止ciq带出错误
    }
    
  //  $("#goodSelect_hsbm").combogrid("showPanel").combogrid("hidePanel");//防止ciq带出错误.
   
}

function updateGoodTr() {
    if (addOrUpdateGoodValidate() != 1) {
        return 0;
    }
    updateTr("good_input_table", good_edit_serid);
    setXkzDivHtml(good_edit_serid);
    setHwxxjlDivHtml(good_edit_serid);
    initGoodInput();
    validateGoodsTable("good_show_tble");
    clearJyBjslAndBjzlRequredClass();
    focusJyAndGjHSCode();
    reSetShowSerid();
    return 1;
}

function initGoodInput() {
    setGoodSerId(goodSerIdIndex);
    good_edit_serid = "";
    $("#good_addButton").val("添加");
    clearInputBeforeTr("good_input_table", 1);
    $("#goodSelect_hsbm").next('span').find('input').focus();
}

function changeBZL(self) {
    //$("#"+JFLXParam).attr("value",$(self).attr("value"));
    if ($(self).attr("id") == JFLXParam) {
        $("#good_bzl").attr("value", $(self).attr("value"));
    }
}

function caculateGjGoodPrice() {
    caculatePriceBase("good_hwzz", JFLXParam, "good_hwdj");
}
function setJyjyReturnValue(jyjy) {
    $("#jyjyInput").val("value", jyjy.id);
    $("#gjhInput").attr("value", jyjy.gjh);
    $("#gjglmmInput").attr("value", jyjy.gjglmm);
    $("#qsbhInput").attr("value", jyjy.qsbh);
    $("#bjhInput").attr("value", jyjy.bjh);
    $("#jyjyId").val(jyjy.id);
    $("#tgdhInput").val(jyjy.tgdh);
    $("#JYISLOCK").val(jyjy.islock);
    $('#JYVERSION_NO').val(jyjy.version_no);
    if (jyjy.bjsbrq != null) {
        $("#bjsbrqInput").val(toDate(jyjy.bjsbrq));
    }
    changeGjStatuName(jyjy.state);
}

function setJyjyReturnGoodListValue(goodList){
	var tmp;
	for(var index = 0 ; index < goodList.length ; index ++){
		tmp  = goodList[index];
		$("#good_id_"+tmp.index).attr("value",tmp.id);
		$("#good_spxh_"+tmp.index).attr("value",tmp.hwxh);
	}
	
}

function setJyjyReturnBoxListValue(boxList){
	var tmpBox;
	for(var index = 0 ; index < boxList.length ; index ++){
		tmpBox  = boxList[index];
		$("#box_id_"+tmpBox.index).attr("value",tmpBox.id);
	}
			
	
}

//---计算特管区
function calculateBase(totalId, addConstanId, addLikeId, showSpanId) {
    var bzlTotal = $("#" + totalId).val();
    var inputTotal = 0;
    $("#" + addConstanId + " input[id^='" + addLikeId + "']").each(function() {
        var value = $(this).val();
        if (value && value != "") {
            inputTotal += Number(value);
        }
    });
    bzlTotal = bzlTotal - inputTotal;
    $("#" + showSpanId).html(bzlTotal);
    if (inputTotal == 0) {
        bzlTotal = 0;
    }
    return bzlTotal;
}
//---------------国检公共 END-----------------

//=======================关检联合界面公共Start======================//
function addOrUpdateHGGJGood() {
	var flag;
    if (good_edit_serid != null && good_edit_serid != "") {
        flag = updateHGGJGoodTr();
    } else {
    	var jylock = $("#JYISLOCK").val();
    	var hglock = $("#HGISLOCK").val();
    	 if(jylock == "1" || hglock == "1"){
    		 $.messager.alert('提示',"该数据正在申报,不可添加商品.");
    		return;
    	 }
      flag = addHGGJGood();
    }
    //
    if(flag == 1){
    	if($("#ciqSelect").attr("isinit") && $("#ciqSelect").attr("isinit") == 1) {
            $("#ciqSelect").combogrid("clear");
        }
    }
}
function addHGGJGood() {
    if (addOrUpdateGoodValidate() != 1) {
        return 0;
    }
    var addHtml = getNewHGGJGoodHtml(goodSerIdIndex);
    $("#good_lastTR").before(addHtml);
    backUpXkzId(goodSerIdIndex);
    backUpHwxxjlId(goodSerIdIndex);
    goodSerIdIndex = goodSerIdIndex + 1;
    setGoodSerId(goodSerIdIndex);
    changeGoodXkzInput();
    clearInputBeforeTr("good_input_table", 1);
    $("#hggoods_spggxh").attr("value","0");
    reSetShowSerid();
    initHgDestinationCountry();
    noticesHgGoodCounts();
    return 1;
}

function getNewHGGJGoodHtml(serid) {
    var result = "<tr id='good_TR" + serid + "' onclick='editGoodTr(" + serid + ")'>";
    var namePre = "baHgbaseinfo.inspectAndDeclareGoods[" + serid + "].";
    var hgNamePre = namePre + "baHggoods.";
    var jyjyNamePre = namePre + "jyjygoods.";
    result += addTr("jyjyTbody", serid, jyjyNamePre);
    result += addTr("hgGoodTbody", serid, hgNamePre);
    result += addTr("gjGoodTbody", serid, hgNamePre);
    result += "<td  style='text-align:center;'><img src=\"../image/delete.gif\" onclick=\"deleteJyGoodTr("+serid +")\">";
    result += getXkzDivHtml(serid);
    result += getHwxxjlDivHtml(serid) + "</td>";
    result += "</tr>";
    return result;
}

function updateHGGJGoodTr() {
   var result =  updateGoodTr();
   initHgDestinationCountry();
   if(result == 1 ){
	   clearHgDejldwRequredClass();
	    $("#hggoods_spggxh").attr("value","0");
	    return 1;
   }
   return 0; 
}

function caclulateHgAndGjGoodPrice() {
    caculatePriceBase("hggoods_zj", "hggoods_sbsl", "hggoods_dj");
}

//=======================关检联合界面公共END======================//
